package com.trm.visit.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.trm.role.dao.RoleDao;
import com.trm.student.service.StudentService;

public class VisitServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String methodName = request.getParameter("action");
        if (methodName == null || "".equals(methodName)) {
            methodName = "roleList";
        }
        try {
            Method method = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this.getClass().newInstance(), request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    /**
     * 访客首页
     * 
     * @param request
     * @param response
     */
    public void roleList(HttpServletRequest request, HttpServletResponse response) {
        RoleDao roleDao = new RoleDao();
        List<Map<String, Object>> list = roleDao.getRoleList();
        request.setAttribute("list", list);
        String userAgent = request.getHeader("user-agent");
        String type = "PC";
        if (userAgent.toLowerCase().contains("android") || (userAgent.toLowerCase().contains("iphone"))
                || (userAgent.toLowerCase().contains("phone"))) {
            type = "mobile";
        }
        request.setAttribute("type", type);
        try {
            request.getRequestDispatcher("/visit/home.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加信息页面
     * 
     * @param request
     * @param response
     */
    public void add(HttpServletRequest request, HttpServletResponse response) {
        String role = request.getParameter("role");
        request.setAttribute("role", role);
        // 获得角色属性
        StudentService stuService = new StudentService();
        List<Map<String, Object>> list = stuService.getProperty(role);
        request.setAttribute("properties", list);
        try {
            request.getRequestDispatcher("/visit/add.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存用户信息
     * 
     * @param request
     * @param response
     */
    public void addUser(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String[]> map = request.getParameterMap();
        StudentService studentService = new StudentService();
        studentService.saveUser(map);
        /*
         * try { request.getRequestDispatcher("/ztree/countrySave.jsp").forward(request,
         * response); } catch (ServletException e) { // TODO Auto-generated catch block
         * e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch
         * block e.printStackTrace(); }
         */
        String url = "info";
        try {
            response.sendRedirect(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
